package web;

import Car.bo.CarBo;
import Car.model.Car;
import util.Context;
import web.beans.CarViewBean;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class CarServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CarServlet() {
        super();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String IsLogout = request.getParameter("id");


            HttpSession session = request.getSession();

            String ResultPage = null; //Страница перехода при успешном
            CarViewBean carViewBean = (CarViewBean) session.getAttribute("carViewBean");
            CarBo carBo = (CarBo) Context.getAppContext().getBean("carBo");
            Car car;
            if (carViewBean == null)
                carViewBean = new CarViewBean();


            //User.setTttt(IsLogout);
            car = carBo.getCarId(new Integer(IsLogout));

            if (car != null) {
                ResultPage = "/Advertisement/carView.xhtml";

                carViewBean.setName_ru(IsLogout);
                carViewBean.setCar(car);
                session.setAttribute("carViewBean", carViewBean);
            }
            else {
                ResultPage="На 404 отправить ";
            }
            ServletContext SC = getServletContext();
            RequestDispatcher Disp = SC.getRequestDispatcher(ResultPage);
            Disp.forward(request, response);
        } catch (Throwable theException) {
            theException.printStackTrace();
        }
    }

}
 